package com.ls.dao;

import com.ls.util.jdbcUtils.JDBCUtilsByDruid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 李朔
 * @version 1.0
 * 开发BasicDAO,是其他DAO的父类
 */
public class BasicDAO<T> {  //泛型指定具体的类型
    //这里使用了Apache-DBUtils
    QueryRunner qr=new QueryRunner();
    //开发通用的dml方法,针对任意的表
    public int update(String sql,Object ... parameters){
        Connection connection=null;
        try {
            connection= JDBCUtilsByDruid.getConnection();
            int affectedRows=qr.update(connection,sql,parameters);
            return affectedRows;
        }
        catch (SQLException e) {
            throw new RuntimeException(e);//将编译异常->运行异常抛出
        }
        finally {
            JDBCUtilsByDruid.close(null,null,connection);
        }
    }
    /**
     * @param sql sql语句，可以含有???
     * @oaram clazz 传入一个类的Class对象,比如Actor.Class等
     * @param parameters  可变形参，传入?的具体的值，可以是多个
     * @return 根据Actor.class 返回对应的ArrayList集合
     */
    //返回多个对象（即查询的结果是多行的）,针对任意表
    public List<T> queryMultiply(String sql,Class<T> clazz,Object ... parameters){
            Connection connection=null;
            try {
                connection= JDBCUtilsByDruid.getConnection();
                List<T> query = qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);
                return query;
            }
            catch (SQLException e) {
                throw new RuntimeException(e);//将编译异常->运行异常抛出
            }
            finally {
                JDBCUtilsByDruid.close(null,null,connection);
            }
    }

    //查询单行结果的通用方法
    public T querySingle(String sql,Class<T> clazz,Object ... parameters){
        Connection connection=null;
        try {
            connection= JDBCUtilsByDruid.getConnection();
            T query = qr.query(connection, sql,new BeanHandler<T>(clazz), parameters);
            return query;
        }
        catch (SQLException e) {
            throw new RuntimeException(e);//将编译异常->运行异常抛出
        }
        finally {
            JDBCUtilsByDruid.close(null,null,connection);
        }
    }
    //查询单行结果的通用方法
    public ResultSet querySingle_new(Connection connection,PreparedStatement preparedStatement,String sql,Object[] parameters){

        try{
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < parameters.length; i++) {
                preparedStatement.setObject(i+1,parameters[i]);
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            return resultSet;
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    //查询单行单列的方法，即返回单值的方法
    public Object queryScalar(String sql,Object ... parameters){
        Connection connection=null;
        try {
            connection= JDBCUtilsByDruid.getConnection();
            Object query = qr.query(connection, sql,new ScalarHandler(), parameters);
            return query;
        }
        catch (SQLException e) {
            throw new RuntimeException(e);//将编译异常->运行异常抛出
        }
        finally {
            JDBCUtilsByDruid.close(null,null,connection);
        }
    }

}
